<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>薪资添加</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{../../lib/layui/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{../../css/public.css}" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <label class="layui-form-label required">部门</label>
        <div class="layui-input-block">
            <select name="dept_name" lay-verify="required" lay-filter="empIdFilter">
                <option value="">请选择</option>
                <option th:each="dept:${deptList}" th:value="${dept.id}">
                    <span th:text="${dept.name}"></span>
                </option>
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">员工</label>
        <div class="layui-input-block">
            <select id="employee_name" name="employee_name" lay-verify="required">
                <option value="">请选择</option>
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">发放日期</label>
        <div class="layui-input-inline">
            <input type="text" id="creattime" name="creattime" autocomplete="off" class="layui-input date">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">基本薪资</label>
        <div class="layui-input-inline">
            <input type="text" id="base_salary" name="base_salary" autocomplete="off" class="layui-input date">
        </div>
    </div>


    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">薪资奖赏</label>
            <div class="layui-input-inline">
                <input type="text" name="ad_salary" autocomplete="off" class="layui-input" id="ad_salary">
            </div>
        </div>
    </div>


    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">添加薪资</button>
        </div>
    </div>
</div>
<script th:src="@{../../lib/layui/layui.js}" charset="utf-8"></script>
<script th:src="@{../../js/lay-config.js?v=2.0.0}" charset="utf-8"></script>
<script th:src="@{../../lib/jquery-3.4.1/jquery-3.4.1.min.js}" charset="utf-8"></script>

<script>
    function convertStatus(data) {
        if (data.status == '1') {

            return '<a class="layui-btn layui-btn-xs layui-btn-normal">奖励</a>';
        } else if (data.status == '0') {
            return '<a class="layui-btn layui-btn-xs layui-btn-danger">惩罚</a>';
        }
    }

</script>

<script>
    layui.use(['form','laydate','tableSelect'], function () {
        var form = layui.form,
            laydate = layui.laydate,
            layer = layui.layer,
            tableSelect = layui.tableSelect,
            $ = layui.$;

        laydate.render({
            elem: '#creattime',
            type: 'date'
            //指定元素
        });

        tableSelect.render({
            elem: '#ad_salary',
            checkedKey: 'id',
            searchType: 'more',
            searchList: [
                {searchKey: 'employee_name', searchPlaceholder: '搜索员工姓名'}, //搜索条件1
            ],
            table: {
                url: '/reward/list',
                cols: [
                    [
                        { type: 'radio' },
                        { field: 'id', title: 'ID' },
                        { field: 'employee_name', title: '姓名' },
                        { field: 'price', title: '金额'},
                        {field:'status',title:"类型",templet: convertStatus}
                    ]
                ]
            },
            done: function (elem, data) {
                var NEWJSON = []
                layui.each(data.data, function (index, item) {
                    NEWJSON.push(item.price+"/"+item.status)
                })
                elem.val(NEWJSON.join(","))
            }
        });



        //连锁员工
        form.on('select(empIdFilter)', function (data) {
            $('#employee_name').empty();
            var url = "/emp/queryEmpByDept";
            $.post(url, {dept_id: data.value},
                function (data) {
                    $.each(data, function (i, n) {
                        console.log(n.name);
                        $('#employee_name').append(new Option(n.name, n.id));// 下拉菜单里添加元素
                    });
                    layui.form.render("select");//重新渲染 固定写法
                });
        });






        form.on('submit(saveBtn)', function (data) {
            console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
            $("#btn-submit").attr("disabled", "disabled").addClass("layui-btn-disabled");
            //提交数据
            var url = "/salary/add";
            $.post(url, data.field, function (res) {
                console.log(res);
                if (res.code === 0) {
                    //成功提示
                    layer.msg(res.message, {
                        icon: 1,
                        time: 2000 //2秒关闭（如果不配置，默认是3秒）
                    }, function () {
                        //关闭窗口,刷新数据表格
                        parent.refreshTable();
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭
                    });
                } else {
                    $("#btn-submit").removeAttr("disabled", "disabled").removeClass("layui-btn-disabled");
                    //失败则提示错误消息,窗口不关闭
                    layer.msg('新增部门失败,请联系管理员', {
                        icon: 2,
                        time: 2000, //2秒关闭（如果不配置，默认是3秒）
                        anim: 6
                    }, function () {
                        //do something
                    });
                }
            })
            //关掉窗口并刷新
            //失败则提示错误信息
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
    });
</script>
</body>
</html>